﻿#include "polarfeatureitem.h"

#include <QDebug>
#include <QGraphicsScene>

PolarFeatureItem::PolarFeatureItem(const QStringList& machineList, QObject *parent, QGraphicsItem *item) : QObject(parent), QGraphicsItem(item),
    m_machineList(machineList)
{
    //缓存在设备坐标系中，并且只有在位置、大小或外观发生变化时才需要重新计算。
    this->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
}

QRectF PolarFeatureItem::boundingRect() const
{
    return QRectF(0, 0, scene()->width(), 160);
}

void PolarFeatureItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);
    painter->setCompositionMode(QPainter::CompositionMode_Source);

    QPen pen;
    pen.setColor(Qt::black);
    painter->setPen(pen);
    painter->setFont(QFont("Helvetica", 12));

    // 绘制网格线
    painter->drawLine(QLineF(30, 0, 870, 0));
    painter->drawLine(QLineF(30, 40, 870, 40));
    painter->drawLine(QLineF(30, 160, 870, 160));

    painter->drawLine(QLineF(30, 0, 30, 160));
    painter->drawLine(QLineF(80, 40, 80, 160));
    painter->drawLine(QLineF(130, 40, 130, 160));
    for (int i = 0; i < 5; i++)
        painter->drawLine(QLineF(260 + i * 120, 0, 260 + i * 120, 160));
    painter->drawLine(QLineF(870, 0, 870, 160));

    // 文字说明
    painter->drawText(QPointF(100, 25), QStringLiteral("项目或参数"));
    painter->drawText(QPointF(290, 25), QStringLiteral("I作动器"));
    painter->drawText(QPointF(410, 25), QStringLiteral("II作动器"));
    painter->drawText(QPointF(530, 25), QStringLiteral("III作动器"));
    painter->drawText(QPointF(650, 25), QStringLiteral("IV作动器"));
    painter->drawText(QPointF(770, 25), QStringLiteral("技术要求"));
    painter->drawText(QPointF(45, 65), QStringLiteral("极"));
    painter->drawText(QPointF(45, 90), QStringLiteral("性"));
    painter->drawText(QPointF(45, 115), QStringLiteral("检"));
    painter->drawText(QPointF(45, 140), QStringLiteral("查"));
    painter->drawText(QPointF(95, 65), QStringLiteral("信"));
    painter->drawText(QPointF(95, 90), QStringLiteral("号"));
    painter->drawText(QPointF(95, 115), QStringLiteral("位"));
    painter->drawText(QPointF(95, 140), QStringLiteral("移"));
    painter->drawText(QPointF(140, 100), QStringLiteral("极性"));
    painter->drawText(QPointF(750, 100), QStringLiteral("极性正确"));

    // 文字数值: 1, 2, 3, 4
    if (m_machineList.contains("A1"))
        painter->drawText(QPointF(290, 100), QStringLiteral("正确"));
    if (m_machineList.contains("A2"))
        painter->drawText(QPointF(410, 100), QStringLiteral("正确"));
    if (m_machineList.contains("A3"))
        painter->drawText(QPointF(530, 100), QStringLiteral("正确"));
    if (m_machineList.contains("A4"))
        painter->drawText(QPointF(650, 100), QStringLiteral("正确"));
}
